package santiago.view.menu;

import java.net.InetAddress;
import java.net.UnknownHostException;
import santiago.client.Client;
import santiago.view.SantiagoDialog;
import santiago.view.SantiagoValidation;

public class DialogJoinServer extends SantiagoDialog {

    /**
     * Creates new form DialogJoinServer
     */
    public DialogJoinServer(java.awt.Frame Parent, boolean Modal) {
        super(Parent, Modal);
        initComponents();
        try {
            //Init the IP field to the local one
            TextFieldIP.setText(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException ex) {
        }
        setLocationRelativeTo(null);
    }

    @Override
    public void refresh() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LabelIP = new javax.swing.JLabel();
        TextFieldIP = new javax.swing.JTextField();
        javax.swing.JButton ButtonConnect = new javax.swing.JButton();
        ButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Rejoindre un serveur");
        setMinimumSize(new java.awt.Dimension(400, 190));
        setResizable(false);

        LabelIP.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelIP.setText("Adresse IP");

        TextFieldIP.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N

        ButtonConnect.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        ButtonConnect.setForeground(new java.awt.Color(179, 96, 74));
        ButtonConnect.setText("Se connecter");
        ButtonConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonConnectActionPerformed(evt);
            }
        });

        ButtonCancel.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        ButtonCancel.setForeground(new java.awt.Color(179, 96, 74));
        ButtonCancel.setText("Annuler");
        ButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(LabelIP)
                        .addGap(18, 18, 18)
                        .addComponent(TextFieldIP, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(ButtonConnect)
                        .addGap(18, 18, 18)
                        .addComponent(ButtonCancel)))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(LabelIP, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(TextFieldIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ButtonConnect)
                    .addComponent(ButtonCancel))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(364, 132));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonConnectActionPerformed
        if (SantiagoValidation.checkIP(TextFieldIP.getText())) {
            Client.connect(TextFieldIP.getText());
        }
    }//GEN-LAST:event_ButtonConnectActionPerformed

    private void ButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelActionPerformed
        Client.getInterface().closeDialog();
    }//GEN-LAST:event_ButtonCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCancel;
    private javax.swing.JLabel LabelIP;
    private javax.swing.JTextField TextFieldIP;
    // End of variables declaration//GEN-END:variables
}
